package com.hippo.core.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.google.gson.Gson
import com.hippo.core.state.DeviceValue
import com.hippo.core.ui.compose.control.CleanScreen
import com.hippo.core.ui.compose.control.ConsumablesScreen
import com.hippo.core.ui.compose.control.ControlScreen
import com.hippo.core.ui.compose.control.ControlSettingScreen
import com.hippo.core.ui.compose.home.AddDeviceScreen
import com.hippo.core.ui.compose.home.ScanDeviceScreen
import com.hippo.core.ui.compose.login.LoginScreen
import com.hippo.core.ui.compose.login.WaterEnviScreen
import com.hippo.core.ui.compose.login.forget.ResetPasswordScreen
import com.hippo.core.ui.compose.login.forget.ResetSuccessScreen
import com.hippo.core.ui.compose.login.forget.VerifyCodeScreen
import com.hippo.core.ui.compose.login.forget.VerifyEmailScreen
import com.hippo.core.ui.compose.main.MainScreen
import com.hippo.core.ui.compose.mine.AboutScreen
import com.hippo.core.ui.compose.mine.DeviceSettingScreen
import com.hippo.core.ui.compose.mine.LanguageScreen
import com.hippo.core.ui.compose.mine.NotificationScreen
import com.hippo.core.ui.compose.protocol.ProtocolScreen
import com.hippo.core.ui.compose.register.RegisterScreen
import com.hippo.core.ui.compose.setting.AccountScreen
import com.hippo.core.ui.compose.setting.MessageSettingScreen
import com.hippo.core.ui.compose.setting.SettingScreen
import com.hippo.core.ui.compose.splash.SplashScreen
import com.hippo.core.ui.compose.spt.FaqDetailScreen
import com.hippo.core.ui.compose.spt.ProductDetailScreen
import com.hippo.core.ui.compose.spt.UserManualScreen
import com.hippo.core.ui.compose.web.WebScreen
import com.hippo.core.vm.MainVM

@Composable
fun AppNavHost(
    vm: MainVM,
    navController: NavHostController,
    modifier: Modifier = Modifier,
) {

    NavHost(
        modifier = modifier,
        navController = navController,
        startDestination = AppRoute.SPLASH
    ) {

        composable(AppRoute.SPLASH) {
            SplashScreen(navController)
        }

        composable(AppRoute.REGISTER) {
            RegisterScreen(
                onBack = {
                    navController.popBackStack()
                },
                onMain = {
                    navController.navigate(AppRoute.MAIN)
                }
            )
        }

        composable(AppRoute.LOGIN) {
            LoginScreen(
                toMain = {
                    navController.navigate(AppRoute.MAIN){
                        popUpTo(AppRoute.LOGIN){
                            inclusive = true
                        }
                    }
                },
                toForgetPassword = {
                    navController.navigate(AppRoute.VerifyEmailScreen)
                },
                toRegistry = {
                    navController.navigate(AppRoute.REGISTER)
                }
            )
        }


        composable(AppRoute.VerifyEmailScreen) {
            VerifyEmailScreen(
                onBack = {
                    navController.popBackStack()
                }, onNext = {
                    navController.navigate(AppRoute.VerifyCodeScreen.replace("{email}", it))
                }
            )
        }

        composable(route = AppRoute.VerifyCodeScreen, arguments = listOf(
            navArgument("email") {
                type = NavType.StringType
            }
        )) {
            val email = it.arguments?.getString("email")
            VerifyCodeScreen(
                email = email.orEmpty(),
                onBack = {
                    navController.popBackStack()
                }, onNext = {email,code->
                    navController.navigate(AppRoute.ResetPasswordScreen.replace("{email}", email).replace("{code}", code))
                }
            )
        }

        composable(route = AppRoute.ResetPasswordScreen) {

            val email = it.arguments?.getString("email")
            val code = it.arguments?.getString("code")

            ResetPasswordScreen(
                email = email.orEmpty(),
                code = code.orEmpty(),
                onBack = {
                    navController.popBackStack()
                }, onNext = {
                    navController.navigate(AppRoute.ResetSuccessScreen)
                })
        }


        composable(AppRoute.ResetSuccessScreen) {
            ResetSuccessScreen(
                onBack = {
                    navController.popBackStack()
                },
                toLogin = {
                    navController.navigate(AppRoute.LOGIN) {
                        popUpTo(AppRoute.LOGIN) {
                            inclusive = true
                        }
                    }
                })
        }


        composable(AppRoute.MAIN) {
            MainScreen(vm,navController)
        }

        composable(AppRoute.WaterEnviScreen) {
            val value = it.arguments?.getString("deviceValue")
            val deviceValue = try {
                Gson().fromJson(value, DeviceValue::class.java)
            } catch (e: Exception) {
                DeviceValue()
            }
            WaterEnviScreen(
                deviceValue = deviceValue,
                onBack = {
                    navController.popBackStack()
                },
            )
        }


        composable(
            route = AppRoute.FaqDetailScreen,
            arguments = listOf(
                navArgument("faqId") {
                    type = NavType.IntType
                }
            )
        ) {
            val faqId = it.arguments?.getInt("faqId") ?: 0
            FaqDetailScreen(faqId, onBack = {
                navController.popBackStack()
            })
        }
        composable(
            route = AppRoute.ProductDetailScreen,
            arguments = listOf(
                navArgument("productId") {
                    type = NavType.IntType
                }
            )
        ) {
            val productId = it.arguments?.getInt("productId") ?: 0
            ProductDetailScreen(
                productId = productId,
                onBack = {
                    navController.popBackStack()
                },
                onFaqClick = {
                    navController.navigate(AppRoute.FaqDetailScreen.replace("{faqId}", it.toString()))
                },
                onWatch = {
                    navController.navigate(AppRoute.UserManualScreen.replace("{url}", it))
                }
            )
        }

        composable(AppRoute.ScanDeviceScreen) {
            ScanDeviceScreen(onBack = {
                navController.popBackStack()
            })
        }

        composable(AppRoute.CONTROL) {
            val devId = it.arguments?.getString("devId").orEmpty()
            ControlScreen(
                devId = devId,
                onBack = {
                    navController.popBackStack()
                },
                onSettingClick = {
                    navController.navigate(AppRoute.ControlSettingScreen)
                },
                onMonitorClick = { deviceValue->
                    navController.navigate(AppRoute.WaterEnviScreen.replace("{deviceValue}", deviceValue))
                }
            )
        }

        composable(AppRoute.ControlSettingScreen) {
            ControlSettingScreen(navController)
        }

        composable(
            route = AppRoute.UserManualScreen,
            arguments = listOf(
                navArgument("url") {
                    type = NavType.StringType
                }
            )
        ) {

            UserManualScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }

        composable(AppRoute.AboutScreen) {
            AboutScreen(navController)
        }

        composable(AppRoute.AccountScreen) {
            AccountScreen(
                onBack = {
                    navController.popBackStack()
                },
                onLogin = {
                    navController.navigate(AppRoute.LOGIN) {
                        popUpTo(AppRoute.LOGIN) {
                            inclusive = true
                        }
                    }
                },
                toHelp ={
                    navController.navigate(AppRoute.FaqDetailScreen.replace("{faqId}", "1"))
                },
                toForgetPW = {
                    navController.navigate(AppRoute.VerifyEmailScreen)
                }
            )
        }


        composable(AppRoute.NotificationScreen) {
            NotificationScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        composable(AppRoute.MessageSettingScreen) {
            MessageSettingScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }

        composable(AppRoute.DeviceSettingScreen) {
            DeviceSettingScreen(navController)
        }

        composable(AppRoute.LanguageScreen) {
            LanguageScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        composable(AppRoute.SettingScreen) {
            SettingScreen(navController)
        }
        composable(AppRoute.PROTOCOL) {
            ProtocolScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }

        composable(AppRoute.WebScreen) {
            WebScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        composable(AppRoute.CleanScreen) {
            CleanScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        composable(AppRoute.AddDeviceScreen) {
            AddDeviceScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        composable(AppRoute.ConsumablesScreen) {
            ConsumablesScreen(
                onBack = {
                    navController.popBackStack()
                }
            )
        }



    }

}